package org.tbjd.client.mediators;

import java.util.HashMap;
import java.util.Map;

import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.NoUpperMediatorFoundException;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.exceptions.TaskManagerException;
import org.tbjd.client.factories.TaskFactory;
import org.tbjd.client.interfaces.HasMediator;
import org.tbjd.client.interfaces.Observer;
import org.tbjd.client.managers.TaskManager;

public class TaskListMediator extends AbstractMediator implements Observer, HasMediator {

	private AbstractMediator mediator = null;

	private TaskManager taskManager = new TaskManager();

	public void decide(Object action) {
		System.out.println("TaskListMediator.decide()");
		handle(action);

	}

	public void delegate(Object action) throws NoUpperDecisionmakerFoundException, NoUpperMediatorFoundException {
		System.out.println("TaskListMediator.delegate()");

		try {
			this.mediator.decide(action);
		} catch (TBJDException e) {
			e.printStackTrace();
		}
	}

	public void handle(Object action) {
		System.out.println("TaskListMediator.handle()");

		// Received the select item
		if (((Map<?, ?>) action).containsKey(NotificationsConstants.TASK_ADDED)) {

			((Map<Integer, Object>) action).put(NotificationsConstants.TASKMANAGER_ACTION_add, null);
			
			try {
				Map<Integer, Object> mapResults = new HashMap<Integer, Object>();
				mapResults.put(NotificationsConstants.TASKMANAGER_RESULT, taskManager.managePositions((Map<Integer, Object>) action));

				delegate(mapResults);
			} catch (TaskManagerException e) {
				e.printStackTrace();
			} catch (NoUpperDecisionmakerFoundException e) {
				e.printStackTrace();
			} catch (NoUpperMediatorFoundException e) {
				e.printStackTrace();
			}
		} else if (((Map<?, ?>) action).containsKey(NotificationsConstants.TASK_DELETED)) {

			Task selectedTask = ((Task) ((Map<?, ?>) action).get(NotificationsConstants.TASK_DELETED));

			Map<Integer, Object> map = new HashMap<Integer, Object>();
			map.put(NotificationsConstants.TASKMANAGER_ACTION_delete, selectedTask);

			try {
				Map<Integer, Object> mapResults = new HashMap<Integer, Object>();
				mapResults.put(NotificationsConstants.TASKMANAGER_RESULT, taskManager.managePositions(map));
				mapResults.put(NotificationsConstants.TASKMANAGER_RESULT_rowsToRemove, selectedTask.getLstChildren().size() + 1);

				delegate(mapResults);
			} catch (TaskManagerException e) {
				e.printStackTrace();
			} catch (NoUpperDecisionmakerFoundException e) {
				e.printStackTrace();
			} catch (NoUpperMediatorFoundException e) {
				e.printStackTrace();
			}
		} else if (((Map<?, ?>) action).containsKey(NotificationsConstants.TASK_TYPE_SWITCHED)) {

			Task selectedTask = ((Task) ((Map<?, ?>) action).get(NotificationsConstants.TASK_TYPE_SWITCHED));

			Map<Integer, Object> map = new HashMap<Integer, Object>();
			map.put(NotificationsConstants.TASKMANAGER_ACTION_typeSwitched, selectedTask);

			try {
				Map<Integer, Object> mapResults = new HashMap<Integer, Object>();
				mapResults.put(NotificationsConstants.TASKMANAGER_RESULT, taskManager.managePositions(map));

				delegate(mapResults);
			} catch (TaskManagerException e) {
				e.printStackTrace();
			} catch (NoUpperDecisionmakerFoundException e) {
				e.printStackTrace();
			} catch (NoUpperMediatorFoundException e) {
				e.printStackTrace();
			}
		} else if (((Map<?, ?>) action).containsKey(NotificationsConstants.DATEMANAGER_ACTION_duration)) {
			try {
				Map<Integer, Object> mapResults = new HashMap<Integer, Object>();
				mapResults.put(NotificationsConstants.TASKMANAGER_RESULT, taskManager.manageDates(((Map<Integer, Object>) action)));

				delegate(mapResults);
			} catch (TaskManagerException e) {
				e.printStackTrace();
			} catch (NoUpperDecisionmakerFoundException e) {
				e.printStackTrace();
			} catch (NoUpperMediatorFoundException e) {
				e.printStackTrace();
			}
		} else if (((Map<?, ?>) action).containsKey(NotificationsConstants.DATEMANAGER_ACTION_progress)) {
			System.out.println("TreeGrid.decide() DATEMANAGER_ACTION_progress");
			try {
				Map<Integer, Object> mapResults = new HashMap<Integer, Object>();
				mapResults.put(NotificationsConstants.TASKMANAGER_RESULT, taskManager.manageDates(((Map<Integer, Object>) action)));

				delegate(mapResults);
			} catch (TaskManagerException e) {
				e.printStackTrace();
			} catch (NoUpperDecisionmakerFoundException e) {
				e.printStackTrace();
			} catch (NoUpperMediatorFoundException e) {
				e.printStackTrace();
			}
		} else if (((Map<?, ?>) action).containsKey(NotificationsConstants.DATEMANAGER_ACTION_beginDate)) {
			System.out.println("TreeGrid.decide() DATEMANAGER_ACTION_beginDate");
			try {
				Map<Integer, Object> mapResults = new HashMap<Integer, Object>();
				mapResults.put(NotificationsConstants.TASKMANAGER_RESULT, taskManager.manageDates(((Map<Integer, Object>) action)));

				delegate(mapResults);
			} catch (TaskManagerException e) {
				e.printStackTrace();
			} catch (NoUpperDecisionmakerFoundException e) {
				e.printStackTrace();
			} catch (NoUpperMediatorFoundException e) {
				e.printStackTrace();
			}
		} else if (((Map<?, ?>) action).containsKey(NotificationsConstants.DATEMANAGER_ACTION_endDate)) {
			System.out.println("TreeGrid.decide() DATEMANAGER_ACTION_endDate");
			try {
				Map<Integer, Object> mapResults = new HashMap<Integer, Object>();
				mapResults.put(NotificationsConstants.TASKMANAGER_RESULT, taskManager.manageDates(((Map<Integer, Object>) action)));

				delegate(mapResults);
			} catch (TaskManagerException e) {
				e.printStackTrace();
			} catch (NoUpperDecisionmakerFoundException e) {
				e.printStackTrace();
			} catch (NoUpperMediatorFoundException e) {
				e.printStackTrace();
			}
		}
	}

	public void update(Object sender) {
		System.out.println("TaskListMediator.update()");
		notifyObservers(sender);
	}

	public void setMediator(AbstractMediator mediator) {
		this.mediator = mediator;
		this.mediator.addObserver(this);
	}

}
